package edu.uni.dormitory.service.impl;

import edu.uni.dormitory.bean.*;
import edu.uni.dormitory.mapper.MaintenanceApplyApprovalMapper;
import edu.uni.dormitory.mapper.MaintenanceApplyMapper;
import edu.uni.dormitory.mapper.MaintenanceEvaluationMapper;
import edu.uni.dormitory.mapper.RoomMaintenanceMapper;
import edu.uni.dormitory.service.MaintenanceService;
import edu.uni.dormitory.service.StudentBedService;
import edu.uni.userBaseInfo1.bean.ApprovalStepIncharge;
import edu.uni.userBaseInfo1.bean.Student;
import edu.uni.userBaseInfo1.service.ApprovalMainService;
import edu.uni.userBaseInfo1.service.ApprovalStepInchargeService;
import edu.uni.userBaseInfo1.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 郑伟森
 * @create 2019.05.01
 * @function 服务
 */
@Service
public class MaintenanceServiceImpl implements MaintenanceService{
    @Autowired
    private MaintenanceApplyMapper maintenanceApplyMapper;
    @Autowired
    private MaintenanceApplyApprovalMapper maintenanceApplyApprovalMapper;
    @Autowired
    private RoomMaintenanceMapper roomMaintenanceMapper;
    @Autowired
    private MaintenanceEvaluationMapper maintenanceEvaluationMapper;
    @Autowired
    private ApprovalMainService approvalMainService;
    @Autowired
    private ApprovalStepInchargeService approvalStepInchargeService;

    @Autowired
    private StudentService studentService;
    @Autowired
    private StudentBedService studentBedService;

    @Override
    public MaintenanceApply selectMaintenanceApplyById(Long id) {
        MaintenanceApply maintenanceApply = maintenanceApplyMapper.selectByPrimaryKey(id);
        return maintenanceApply;
    }

    @Override
    public MaintenanceApply selectMaintenanceApplyByIdWithSelf(Long id, Long loginId) {
        //查询的同时判断是不是登录人申请的
        MaintenanceApplyExample maintenanceApplyExample = new MaintenanceApplyExample();
        maintenanceApplyExample.createCriteria().andIdEqualTo(id).andByWhoEqualTo(loginId);
        List<MaintenanceApply> maintenanceApplies = maintenanceApplyMapper.selectByExample(maintenanceApplyExample);
        return maintenanceApplies.size()>0?maintenanceApplies.get(0):null;
    }

    @Override
    public MaintenanceApply selectMaintenanceApplyByIdWithHouseMaster(Long id, Long areaId) {
        return maintenanceApplyMapper.selectMaintenanceApplyByArea(id,areaId);
    }

    @Override
    public List<MaintenanceApply> selectMaintenanceApplyPageByArea(Long areaId) {
        return maintenanceApplyMapper.selectMaintenanceApplyPageByArea(areaId);
    }

    @Override
    public List<MaintenanceApply> selectMaintenanceApplyPageWithSelf(Long loginId) {
        List<Student> students = studentService.selectByUserId(loginId);
        if(students!=null && students.size()>0){
            //获取学生所在宿舍的维修信息
            return maintenanceApplyMapper.selectByRoomWithStuId(students.get(0).getId());
        }
        return null;
    }

    @Override
    public int insertMaintenanceApply(MaintenanceApply maintenanceApply, Long loginId) {
        List<Student> studentList = studentService.selectByUserId(loginId);
        long applyPerson = (studentList==null||studentList.size()==0)?-1:studentList.get(0).getId();
        if(applyPerson == -1) return -1;
        maintenanceApply.setApplyPerson(applyPerson);
        Long mainId  = approvalMainService.selectIdByName((long)1,"maintenance");
        maintenanceApply.setApprovalMainId(mainId);
        maintenanceApply.setByWho(applyPerson);
        maintenanceApply.setDatetime(new Date());
        maintenanceApply.setDeleted((byte)0);
        //通过学生id获取所在宿舍
        Long liveRoomId = studentBedService.selectStudentBedBySid(applyPerson).getLiveRoomId();
        maintenanceApply.setLiveRoomId(liveRoomId);
        int i = maintenanceApplyMapper.insert(maintenanceApply);
        if(i > 0){
            //成功需要获取新建记录的id
            Long stepId = getStepIdByMainId(mainId,1);
            if(updateMaintenanceApply(maintenanceApply.getId(),(byte)1,applyPerson,stepId) > 0L){
                return 1;
            }
        }
        return 0;
    }

    @Override
    public int updateMaintenanceApplyWithHouseMaster(Byte agree, Long areaId, Long loginId, Long id) {
        if(maintenanceApplyApprovalMapper.selectLatestByApplyId(id).getStatus() == 0){
            return -1;
        }
        //新增流程记录
        Long mainId  = approvalMainService.selectIdByName((long)1,"maintenance");
        Long stepId ;
        if(agree == 1){
            //通过ApprovalMainId获取step=3的记录的id
            stepId = getStepIdByMainId(mainId,2);
        }else {
            stepId = getStepIdByMainId(mainId,0);
        }
        if(updateMaintenanceApply(id,agree,loginId,stepId) > 0L){
            return 1;
        }
        return 0;
    }

    @Override
    @Transactional
    public int updateMaintenanceApplyWithInfrastructureSection(RoomMaintenance maintenance, Long loginId, Long id) {
        if(maintenanceApplyApprovalMapper.selectLatestByApplyId(id).getStatus() == 0){
            return -1;
        }//新增流程记录
        Long mainId  = approvalMainService.selectIdByName((long)1,"maintenance");
        Long stepId = getStepIdByMainId(mainId,3);
        Long i = updateMaintenanceApply(id,(byte)1,loginId,stepId);
        if(i > 0L ){
            //新增RoomMaintenance记录
            maintenance.setProcessId(i);
            maintenance.setByWho(loginId);
            maintenance.setDatetime(new Date());
            maintenance.setDeleted((byte)0);
            if(roomMaintenanceMapper.insert(maintenance)>0){
                return 1;
            }
        }
        return 0;
    }

    @Override
    public int insertMaintenanceEvaluation(Long id, MaintenanceEvaluation maintenanceEvaluation, Long loginId) {
        //增加审批记录
        Long mainId  = approvalMainService.selectIdByName((long)1,"maintenance");
        Long stepId = getStepIdByMainId(mainId,4);
        //根据id获取processId
        Long processId = updateMaintenanceApply(id,(byte)1,loginId,stepId);
        if(processId >0L){
            maintenanceEvaluation.setApplyPerson(loginId);
            maintenanceEvaluation.setByWho(loginId);
            maintenanceEvaluation.setDatetime(new Date());
            maintenanceEvaluation.setProcessId(processId);
            maintenanceEvaluation.setDeleted((byte)0);
            return maintenanceEvaluationMapper.insert(maintenanceEvaluation);
        }
        return 0;
    }

    @Override
    public MaintenanceEvaluation selectEvaluationByApplyId(Long applyId) {
        return maintenanceEvaluationMapper.selectEvaluationByApplyId(applyId);
    }

    @Override
    public String selectMaintenanceStatusByApplyId(Long applyId) {
        //获取最新的approvel_step_incharge_id
        Long stepId = maintenanceApplyApprovalMapper.selectLatestByApplyId(applyId).getApprovelStepInchargeId();
        //通过stepId获取步骤名
        return approvalStepInchargeService.selectById(stepId).getName();
    }

    @Override
    public List<MaintenanceApply> selectMaintenanceApplyList() {
        return maintenanceApplyMapper.selectByExample(new MaintenanceApplyExample());
    }

    private Long updateMaintenanceApply(Long applyId, Byte agree, Long loginId, Long stepId) {
        //新增maintenance_apply_approval记录
        MaintenanceApplyApproval maintenanceApplyApproval = new MaintenanceApplyApproval();
        maintenanceApplyApproval.setApplyId(applyId);
        maintenanceApplyApproval.setCheckPerson(loginId);
        maintenanceApplyApproval.setDealTime(new Date());
        maintenanceApplyApproval.setStatus(agree);
        maintenanceApplyApproval.setApprovelStepInchargeId(stepId);
        int i = maintenanceApplyApprovalMapper.insert(maintenanceApplyApproval);
        if(i > 0){
            return maintenanceApplyApproval.getId();
        }
        return 0L;
    }

    private Long getStepIdByMainId(Long mainId,Integer step) {
        List<ApprovalStepIncharge> approvalStepIncharges = approvalStepInchargeService.selectByMainId(mainId);
        for(ApprovalStepIncharge a: approvalStepIncharges){
            if(a.getStep().equals(step)){
                return a.getId();
            }
        }
        return null;
    }
}
